﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using Bierdopje.API;
using Sabnzbd;

namespace Bierdopje.RSS
{
    public class BierdopjeFeedItem
    {
        private ApiLibrary m_API;

        public BierdopjeFeedItem()
        {

        }

        public BierdopjeFeedItem(ApiLibrary apiLibrary)
        {
            m_API = apiLibrary;
        }

        private const string cSubID = @"/sub/(?<SubID>[\d].+?)/";
        private const string cTYUShortPre = @"http://www.bierdopje.com/modalbox/subreplies/";
        private const string cTYUPre = @"http://www.bierdopje.com/?url=http://www.bierdopje.com/thickbox/subreplies/";
        private const string cTYUShortPost = @"";
        private const string cTYUPost = @"/%23/";
        private const string cLinkPostFix = @"/apikey/";
        private static Regex cSubIDReg = new Regex(cSubID, RegexOptions.Compiled);

        private szdFilter m_MatchedFilter;

        public szdFilter MatchedFilter
        {
            get { return m_MatchedFilter; }
            set { m_MatchedFilter = value; }
        }

        public int SubtitleID
        {
            get
            {
                return Convert.ToInt32(cSubIDReg.Match(this.Link).Groups["SubID"].Value);
            }
        }

        public string ThankYouUrl
        {
            get
            {
                return String.Format("{0}{1}{2}", cTYUPre, SubtitleID, cTYUPost);
            }

        }

        public string ThankYouUrlDirect
        {
            get
            {
                return String.Format("{0}{1}{2}", cTYUShortPre, SubtitleID, cTYUShortPost);
            }

        }

        private string m_Title;

        public string Title
        {
            get { return m_Title; }
            set { m_Title = value; }
        }
        private string m_Link;

        public string Link
        {
            get 
            {
                return m_Link; 
            }
            set
            {
                m_Link = value;
                _InfoFromFileName = null;
            }
        }

        public string LinkWithDownloadKey
        {
            get
            {
                if (this.m_API != null)
                {
                    if (!this.m_API.ApiKey.Equals(""))
                    {
                        return m_Link + cLinkPostFix + m_API.ApiKey;
                    }
                }

                throw new EApiLibraryNotPresent();
            }
        }
        private int m_ShowID;

        public int ShowID
        {
            get { return m_ShowID; }
            set { m_ShowID = value; }
        }
        private string m_ShowName;

        public string ShowName
        {
            get { return m_ShowName; }
            set { m_ShowName = value; }
        }
        private int m_Season;

        public int Season
        {
            get { return m_Season; }
            set { m_Season = value; }
        }
        private int m_Episode;

        public int Episode
        {
            get { return m_Episode; }
            set { m_Episode = value; }
        }
        private string m_Uploader;

        public string Uploader
        {
            get { return m_Uploader; }
            set { m_Uploader = value; }
        }
        private string m_Description;

        public string Description
        {
            get { return m_Description; }
            set { m_Description = value; }
        }
        private int m_Size;

        public int Size
        {
            get { return m_Size; }
            set { m_Size = value; }
        }

        public string SEString
        {
            get { return SubHelper.GenerateSEstring(this.Season, this.Episode); }
        }

        public string FileName
        {
            get
            {
                string _FN = HTTPHelper.GetWebFileName(this.Link);
                if (_FN.EndsWith(".srt"))
                    return _FN;
                else
                    return _FN + ".srt";
            }
        }

        public string FileNameWithoutVersionInfo
        {
            get
            {
                return Bierdopje.API.SubHelper.RemoveVersionFromSubtitleFileName(this.FileName);
            }
        }

        private SubHelper.SceneInfo _InfoFromFileName;

        public Bierdopje.API.SubHelper.SceneInfo InfoFromFileName
        {
            get
            {
                if (_InfoFromFileName == null)
                {
                    SubHelper.SceneInfo _SceneInfo = SubHelper.GetInfoFromFileName(this.FileName);
                    if (_SceneInfo == null)
                    {
                        _SceneInfo = new SubHelper.SceneInfo();
                        int ep = SubHelper.GetEpisodeFromFileName(this.FileName);
                        if (ep != -1)
                            _SceneInfo.Episode = ep;
                        else
                            _SceneInfo.Episode = this.Episode;

                        _SceneInfo.Extension = Path.GetExtension(this.FileName);
                        _SceneInfo.MediaType = "";
                        _SceneInfo.Quality = "";

                        int season = SubHelper.GetSeasonFromFileName(this.FileName);
                        if (season != -1)
                            _SceneInfo.Season = season;
                        else
                            _SceneInfo.Season = this.Season;
                        
                        string showName = SubHelper.GetShowNameFromFileName(this.FileName);
                        if (!showName.Equals(""))
                            _SceneInfo.ShowName = showName;
                        else
                            _SceneInfo.ShowName = this.ShowName;
                    }
                    _InfoFromFileName = _SceneInfo;
                    return _SceneInfo;
                }
                else
                    return _InfoFromFileName;
            }
        }

        private ApiShow m_ApiShow;

        public ApiShow ApiShow
        {
            get 
            {
                if (m_API == null)
                    throw new EApiLibraryNotPresent();

                if (m_ApiShow == null)
                {
                    m_ApiShow = m_API.GetShowById(this.ShowID);
                }
                return m_ApiShow;
            }
        }

        private ApiEpisode m_ApiEpisode;

        public ApiEpisode ApiEpisode
        {
            get 
            {
                if (m_ApiEpisode == null)
                {
                    m_ApiEpisode = ApiShow.GetEpisode(this.Season, this.Episode);
                }
                return m_ApiEpisode;
            }
        }

        public bool ApiAlreadyHasNLSub
        {
            get
            {
                ApiEpisode _Ep = ApiEpisode;

                if (_Ep != null)
                    return _Ep.NLSubPresent;
                else
                    return false;
            }
        }

        public ApiShow GetApiShow(ApiLibrary library)
        {
            return library.GetShowById(this.ShowID);
        }

        public ApiEpisode GetApiEpisode(ApiLibrary library)
        {
            ApiShow _Show = this.GetApiShow(library);

            return _Show.GetEpisode(this.Season, this.Episode);
        }

        public ApiEpisode GetApiEpisodeFromSceneInfo(ApiLibrary library)
        {
            ApiShow _Show = this.GetApiShow(library);

            return _Show.GetEpisode(this.InfoFromFileName.Season, this.InfoFromFileName.Episode);
        }

        public bool AlreadyHasNLSub(ApiLibrary library)
        {
            ApiEpisode _Ep = this.GetApiEpisode(library);

            if (_Ep != null)
                return _Ep.NLSubPresent;
            else
                return false;
        }

        public bool AlreadyHasNLSubFromSceneInfo(ApiLibrary library)
        {
            ApiEpisode _Ep = this.GetApiEpisodeFromSceneInfo(library);

            if (_Ep != null)
                return _Ep.NLSubPresent;
            else
                return false;
        }
    }
}
